﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using Common;
using Entity;

namespace DataAccess
{
    /// <summary>
    /// Summary description for CommentDAL
    /// </summary>
    public class CommentDAL : AbstractDataAccess
    {
        List<SqlParameter> _parameters;

        public PaginatedInfo<Comment> GetCommentByArticle(Guid id, int pageIndex, int pageSize)
        {
            var from = ((pageIndex - 1) * pageSize) + 1;
            var to = from + pageSize - 1;
            //Count record
            _parameters = new List<SqlParameter>();
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterArticle, Value = id });
            var totalRecords = (int)ExecuteQueryAsScalar(Constant.GetCountAllCommentByArticleId, _parameters);
            //Paging
            _parameters = new List<SqlParameter>();
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterArticle, Value = id });
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterFrom, Value = from });
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterTo, Value = to });
            var dataTable = ExecuteQueryAsSqlDataTable(Constant.GetAllCommentByArticleId, _parameters);
            //Return paging information
            return new PaginatedInfo<Comment>(dataTable, pageIndex, totalRecords, pageSize);
        }

        public bool Add(Comment comment)
        {
            _parameters = new List<SqlParameter>();
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterId, Value = comment.Id });
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterArticle, Value = comment.Article });
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterPostedPerson, Value = comment.PostedPerson });
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterMessage, Value = comment.Message });
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterPostedDate, Value = comment.PostedDate });

            return ExecuteNonQuery(Constant.CreateComment, _parameters);
        }

        public bool Delete(Guid id)
        {
            _parameters = new List<SqlParameter>();
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterId, Value = id });
            return ExecuteNonQuery(Constant.DeleteComment, _parameters);
        }
    }
}